package com.business_clean.ui.activity.depart;

import static com.business_clean.app.config.Constant.TYPE_INIT_DATA;
import static com.business_clean.app.config.Constant.TYPE_INIT_LOAD_MORE;
import static com.mobile.auth.gatewayauth.utils.ReflectionUtils.getActivity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.ext.CustomViewExtKt;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.app.uitl.CheckListDataUtil;
import com.business_clean.app.uitl.DividerItemDecoration;
import com.business_clean.app.uitl.DownLoadHelper;
import com.business_clean.data.mode.BaseDownLoadEntity;
import com.business_clean.data.mode.project.ProjectManager;
import com.business_clean.data.mode.roster.RosterEntity;
import com.business_clean.databinding.ActivityDepartBinding;
import com.business_clean.ui.activity.personnel.PersonnelDetailActivity;
import com.business_clean.ui.activity.personnel.SelectPersonnelActivity;
import com.business_clean.ui.adapter.depart.DepartAdapter;
import com.business_clean.ui.adapter.roster.RosterRowAdapter;
import com.business_clean.viewmodel.request.DepartViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import me.hgj.mvvmhelper.base.BaseViewModel;

public class DepartActivity extends BaseActivity<DepartViewModel, ActivityDepartBinding> {

    private DepartAdapter mAdapter;
    private RosterRowAdapter rowAdapter;
    private int page = 1;
    private int requestType;
    private String project_uuid;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        mDatabind.tabbar.setRightIcon(R.mipmap.icon_base_export);
        mDatabind.tabbar.setRightSideIcon(R.mipmap.icon_base_add);
        mAdapter = new DepartAdapter();
        CustomViewExtKt.initRecyclerViewVertical(mDatabind.list.recyclerView, mAdapter);

        mDatabind.list.recyclerView.addItemDecoration(new DividerItemDecoration(this, SizeUtils.dp2px(10), SizeUtils.dp2px(10)));

        //row
        rowAdapter = new RosterRowAdapter();
        getMDatabind().recyclerRow.setLayoutManager(new LinearLayoutManager(this));
        getMDatabind().recyclerRow.setAdapter(rowAdapter);

        getMDatabind().list.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestMore();
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestOne();
            }
        });

        mViewModel.getProjectMangerList("2");
    }


    @Override
    public void initObserver() {

    }

    @Override
    public void onRequestSuccess() {
        mViewModel.getStaffManager().observe(this, new Observer<RosterEntity>() {
            @Override
            public void onChanged(RosterEntity data) {
                switch (requestType) {
                    case TYPE_INIT_DATA:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkInitData(data.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.setList(data.getList());
                        }

                        break;
                    case TYPE_INIT_LOAD_MORE:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkLoadMoreData(data.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.addData(data.getList());
                        }
                        break;
                }
            }
        });

        mViewModel.getDownLoadEntity().observe(this, new Observer<BaseDownLoadEntity>() {
            @Override
            public void onChanged(BaseDownLoadEntity baseDownLoadEntity) {
                String fileName = App.appViewModelInstance.getUserInfo().getValue().getCompany().getCompany_name() + "_" + "离职档案" + "_" + TimeUtils.date2String(TimeUtils.getNowDate(), "yyyy-MM-dd-hh-mm");
                DownLoadHelper.downloadFile(DepartActivity.this, baseDownLoadEntity.getDownload_url(), null, fileName, true);
            }
        });


        mViewModel.getAllProjectManager().observe(this, new Observer<ProjectManager>() {
            @Override
            public void onChanged(ProjectManager projectManager) {
                rowAdapter.setList(projectManager.getList());
                if (rowAdapter.getData().size() > 0) {
                    project_uuid = rowAdapter.getData().get(0).getUuid();
                    requestOne();
                }
            }
        });
    }

    @Override
    public void onBindViewClick() {
        //点击
        rowAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                switch (position) {
                    case 0:
//                        project_uuid = "";
//                        is_head_office = "0";
                        break;
                    case 1:
//                        is_head_office = "1";
//                        project_uuid = "";
                        break;
                    default:
//                        is_head_office = "2";
//                        project_uuid = rowAdapter.getData().get(position).getUuid();
                        break;
                }
                project_uuid = rowAdapter.getData().get(position).getUuid();
                rowAdapter.updateItem(position);
                requestOne();
            }
        });

        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
                if (view.getId() == R.id.iv_item_roster_call) {
                    PhoneUtils.dial(mAdapter.getData().get(position).getMobile());
                }
            }
        });

        //详情
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("uuid", mAdapter.getData().get(position).getUuid());
                ActivityForwardUtil.startActivity(PersonnelDetailActivity.class, bundle);
            }
        });

        //点击导出
        mDatabind.tabbar.setOnImageRightTextClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XXPermissions.with(DepartActivity.this)
                        .permission(Permission.WRITE_EXTERNAL_STORAGE).request(new OnPermissionCallback() {
                            @Override
                            public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                                if (allGranted) {
                                    mViewModel.requestDownloadExcel();
                                }
                            }
                        });
            }
        });

        mDatabind.tabbar.setOnImageRightSideClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("is_head_office", 2);//不要总部员工
                bundle.putBoolean("row_visible", true);
                bundle.putString("tab_title", "选择待离职人员");
                ActivityForwardUtil.startActivity(SelectPersonnelActivity.class, bundle);
            }
        });
    }


    void requestOne() {
        requestType = TYPE_INIT_DATA;
        page = 1;
        mViewModel.getStaffList(page, 2, 0, 2, project_uuid);
    }

    void requestMore() {
        requestType = TYPE_INIT_LOAD_MORE;
        page++;
        mViewModel.getStaffList(page, 2, 0, 2, project_uuid);
    }
}